package com.zsh.web.service;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.stereotype.Service;

/**
 * @Author: 003089
 * @Description:
 * @Date: 2019/8/8 9:13
 */
@Service
public class HelloService {


     //@SentinelResource(value = "hello",blockHandler = "exceptionHandler",fallback = "helloFallback")
    //@SentinelResource(value = "hello")
    public String helloService(){
        Entry entry = null;
        try {
            entry = SphU.entry("HelloWorld");
            /*您的业务逻辑 - 开始*/
            return "hello world";
            /*您的业务逻辑 - 结束*/
        } catch (BlockException e1) {
            /*流控逻辑处理 - 开始*/
            System.out.println("block!");
            return "block!";
            /*流控逻辑处理 - 结束*/
        } finally {
            if (entry != null) {
                entry.exit();
            }
        }
    }

    // Fallback 函数，函数签名与原函数一致或加一个 Throwable 类型的参数.
    public String helloFallback() {
        return "block";
    }

    // Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
    public String exceptionHandler(BlockException ex) {
        // Do some log here.
        ex.printStackTrace();
        return "Oops, error occurred at ";
    }

    /*public static void main(String[] args) {
        while (true) {
            Entry entry = null;
            try {
                entry = SphU.entry("HelloWorld");
                *//*您的业务逻辑 - 开始*//*
                System.out.println("hello world");
                *//*您的业务逻辑 - 结束*//*
            } catch (BlockException e1) {
                *//*流控逻辑处理 - 开始*//*
                System.out.println("block!");
                *//*流控逻辑处理 - 结束*//*
            } finally {
                if (entry != null) {
                    entry.exit();
                }
            }
        }
    }*/
}
